package com.jpxx.netty.heartBeat;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class Client {  
	//172.16.2.179
	static final String HOST = System.getProperty("host", "192.168.2.237");  
	static final int PORT = Integer.parseInt(System.getProperty("port", "8765"));  

	public static void main(String[] args) throws Exception {  
		// Configure the client.  
		EventLoopGroup group = new NioEventLoopGroup();  
			Bootstrap b = new Bootstrap();  
			b.group(group)  
			.channel(NioSocketChannel.class)  
			.handler(new LoggingHandler(LogLevel.ERROR))
			.handler(new ChannelInitializer<SocketChannel>() {  
				@Override  
				public void initChannel(SocketChannel sc) throws Exception {  
					 sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());  
		             sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());  
		             sc.pipeline().addLast(new ClientHeartBeatHandler());  
				}  
			});  

			ChannelFuture future = b.connect(HOST, PORT).sync(); //绑定服务端
			future.channel().closeFuture().sync();  
			group.shutdownGracefully();  
	}  

}  